﻿using System.Collections.Generic;
using System.Data;
using Core.Library;

namespace DataObject.Category
{
    public class CategoryController
    {
        /***************************************
        = Back End
        -------------------------------------- */
        public static void Category_Create(CategoryObject o)
        {
            CategoryDAL.Category_Create(o);
        }

        public static void Category_Update(CategoryObject o)
        {
            CategoryDAL.Category_Update(o);
        }

        public static void Category_Delete(int categoryId)
        {
            CategoryDAL.Category_Delete(categoryId);
        }

        public static CategoryObject Category_GetById(int categoryId)
        {
            return ObjectHelper.FillObject<CategoryObject>(CategoryDAL.Category_GetById(categoryId));
        }

        public static DataTable Category_GetListByType_ListId(int typeId, string fieldList, string lstCategory)
        {
            return CategoryDAL.Category_GetListByType_ListId(typeId, fieldList, lstCategory);
        }
       
        public static List<CategoryObject> Category_GetByParentId(int typeId, int parentId, int isActive)
        {
            return ObjectHelper.FillCollection<CategoryObject>(CategoryDAL.Category_GetByParentId(typeId, parentId, isActive));
        } 

        public static List<CategoryObject> PaginatedCollection(List<CategoryObject> categories, int pageIndex, int pageSize)
        {
            var paginatedCollection = new List<CategoryObject>();

            int windowFloor = pageIndex == 1 ? -1 : ((pageSize * pageIndex) - 1) - pageSize >= 0 ? ((pageSize * pageIndex) - 1) - pageSize : 0;

            int windowCeiling = (pageSize * pageIndex) - 1;

            foreach (var i in categories)
            {
                int a = categories.IndexOf(i);
                int b = a;
                if (categories.IndexOf(i) <= windowCeiling && categories.IndexOf(i) > windowFloor)
                    paginatedCollection.Add(i);
            }

            return paginatedCollection;
        }

        public static int Category_GetStatistic(int status)
        {
            return CategoryDAL.Category_GetStatistic(status);
        }


        /***************************************
        = Front End
        -------------------------------------- */
        public static List<CategoryObject> Category_GetByParentId_Filter(bool isFilter, int typeId, int parentId, int isActive)
        {
            return ObjectHelper.FillCollection<CategoryObject>(CategoryDAL.Category_GetByParentId_Filter(isFilter, typeId, parentId, isActive));
        }

        public static string GetCategoryName(int categoryId)
        {
            try { return Category_GetById(categoryId).Name; }
            catch { return string.Empty; }
        }

        public static string GetUnsignCategoryName(int categoryId)
        {
            try { return Category_GetById(categoryId).Url; }
            catch { return string.Empty; }
        }

        public static void GetListTree(List<CategoryObject> arrayList, int typeId, int level, int parentId)
        {
            GetListTree(arrayList, typeId, level, parentId, Constants.ACTIVE);
        }

        public static void GetListTree(List<CategoryObject> arrayList, int typeId, int level, int parentId, int isActive)
        {
            var categories = Category_GetByParentId(typeId, parentId, isActive);

            foreach (var m in categories)
            {
                var prefix = string.Empty;

                for (var i = 0; i < level; i++)
                {
                    prefix += "--- ";
                }

                m.Name = prefix == string.Empty ? string.Format("{0}", m.Name.Trim()) : prefix + m.Name;

                arrayList.Add(m);

                level++;
                GetListTree(arrayList, typeId, level, m.CategoryId);
                level--;
            }
        }
    }
}
